# -*- coding: utf-8 -*-
import os,shutil

'''
遍历2层文件夹，文件名称按照配置文件匹配后，移动到指定目录
'''

file_path = "/Users/chenlong/Downloads/baidudisk/RAZ4"
config_path = "/Users/chenlong/Downloads/baidudisk/configraz4"
file_path_output = "/Users/chenlong/Downloads/baidudisk/RAZ4/output"

class Raz():
    count=0
    matched_list=[]

    def add_num(self):
        list_config=[]
        file = open(config_path,mode='r+',encoding='utf-8',errors=None)
        for i in file:
            list_config.append(i)
        return list_config

    def listdir(self):  #传入存储的list
        if not os.path.exists(file_path_output):
            os.makedirs(file_path_output)

        for file in os.listdir(file_path):
            if not file.__contains__(".DS_Store"):
                file_path1 = os.path.join(file_path, file)
                #print(file_path1)
                for file1 in os.listdir(file_path1):
                    #print(file1)
                    if not file1.__contains__(".DS_Store"):
                        list_contains_result = self.list_is_contains(self.add_num(), file1)
                        left_num=list_contains_result[0].strip().split(".")[0]
                        if list_contains_result[1] == True:
                           print("匹配成功结果==》"+str(list_contains_result))
                           self.matched_list.append(list_contains_result[0].strip())
                           shutil.move(os.path.join(file_path1,file1), os.path.join(file_path_output,"razF-"+left_num+" "+file1))

        #print("全部数据："+str(self.add_num()))
        print("成功匹配数量："+str(self.count))
        print("成功匹配结果："+str(self.matched_list))
        print("失败匹配结果："+str(self.get_diff_list(self.add_num(),self.matched_list)))


    def list_is_contains(self, list1, str_name):
        excel_name=None
        flag=False
        str_name1 = str(str_name).replace(".mp4","").replace(".mp3","")
        for str1 in list1:
            #print(str1,str_name1)
            if str1.lower().strip().__contains__(str_name1.lower().strip()) == True:
                excel_name = str1
                #print(str1,str_name1)
                self.count=self.count+1
                flag = True
                break
        flag_excel_name=[str(excel_name),flag]
        return flag_excel_name

    def get_diff_list(self,mlist,sub_mlist):
        diff_list=[]
        sub_temp_list=[]

        for sub_mlist in sub_mlist:
            sub_temp_list.append(str(sub_mlist).lower().strip())

        for list1 in mlist:
            if not str(list1.lower().strip()) in sub_temp_list:
                diff_list.append(list1.strip())

        return diff_list

if __name__ == '__main__':
    raz = Raz()
    raz.listdir()